var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (t) {
  return typeof t
}
  : function (t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t
  },
  e = getApp(), 
  a = e.requirejs("core"),
  r = e.requirejs("check");
Page({
  data: {
    icons: e.requirejs("icons"),
    list: {},
    cart_list: [],
    address: {},
    data: [],
    showPicker: false,
    pvalOld: [0, 0, 0],
    pval: [0, 0, 0],
    areas: [],
    noArea: true,
    submit: true,
    coupon_display: false,
    disabled: false,
    coupon_num: 0,
    formid: '',
    button_color: e.config.button_color,
    is_mention: 1,//1邮寄，2自提，3自选
    send_type: 'express',
    date: '',
    time: '17:00',
  },

  // 自提、邮寄
  dispatchtype: function(e) {
    var data = a.pdata(e);
    this.setData(data);
  },

  // 日期选择
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var get_time = a.time_str(e.detail.value + ' ' + this.data.time);
    var now_time = Date.parse(new Date()) / 1000;

    if (now_time > get_time) {
      a.error('不小于当前时间');
      return;
    }
    this.setData({
      date: e.detail.value
    })
  },

  // 时间选择
  bindTimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

  onLoad: function (options) {
    a.setting();
    var i = this,
        data=this.data,
        d = {};

    // 订单信息
    var bargain_info = {
      'bargain_name': options.name,
      'original_price': options.current_price,
      'pic': options.pic,
      'goods_id': options.goods_id
    };

    this.setData({
      options: options,
      button_color: getApp().config.button_color,
      font_color: getApp().config.font_color,
      config: getApp().config
    })

    a.get("user/About", {
      user_id: getApp().getCache("userinfo").uid
    }, function (t) {
      if (t.code == 0) {
        var send_type = i.data.send_type;
        if (t.info.is_mention == 2 || t.info.is_mention == 6) {
          send_type = 'selftake';
        } else if (t.info.is_mention == 4) {
          send_type = 'endpay';
        }

        i.setData({
          user_level: t.info.user_level,
          user_rebate: t.info.user_rebate,
          is_mention: t.info.is_mention,
          send_type: send_type
        })
      }
    })

    //获取用户地址
    a.get("Area/UserAddress", { uid: getApp().getCache("userinfo").uid  }, function (t) {
      if (t.code == 0) {
        i.setData({
          address: t.info.address,
          list: bargain_info,
          show: true,
        })
      } else {
        a.alert(t.msg)
      }
      i.caculate()
    })
  },

  onShow: function () {
    var i = this,
        d = e.getCache("orderAddress");
    console.log("333",d);
    if (d) {
      this.setData({
        "address": d,
      })
      i.caculate()
    };
  },

  // 计算金额
  caculate: function (i) {
    console.log(i);
    var e = this,
        a = this.data.address;
    console.log(a,"rrr")
    //订单价
    a.order_money = e.data.options.current_price;
    //运费
    // a.shipping_money = e.data.options.total * e.data.list.goods.shipping_fee;
    // if (t.post_fee != null && t.post_fee != undefined && e.data.send_type != "selftake") {
    //   var post_fee = t.post_fee;
    //   var more = Math.ceil((parseInt(t.num) - parseInt(post_fee.bynum_snum)) / parseInt(post_fee.bynum_xnum));
    //   var more_price = more > 0 ? more * parseFloat(post_fee.bynum_xprice) : 0.0;
    //   shipping_money += parseFloat(post_fee.bynum_sprice) + parseFloat(more_price);
    // }
    //
    // a.pay_money = parseFloat(e.data.options.total * (e.data.list.price - e.data.list.goods.shipping_fee) - discount_money);
    //实付价
    a.pay_money = e.data.options.current_price;
    a.total = e.data.options.total;
    a.user_name = getApp().getCache("userinfo").nickName;
    a.buyer_id = getApp().getCache("userinfo").uid;
   // a.mch_id = e.data.list.mch_id;
    a.activity_order_type = e.data.options.activity_order_type ? e.data.options.activity_order_type : 0;
    a.bargain_id = e.data.options.bargain_id;
    e.setData({
      data: a
    })
  },

  //提交订单
  submit: function (k) {
    var formId = k.detail.formId;
    var e = this,
      t = this.data;

    if (t.send_type == 'selftake') {
      if (!t.data.consigner) {
        a.error('请填写姓名');
        return false;
      }
      if (!t.data.phone) {
        a.error('请填写电话');
        return false;
      }
      if (!t.date) {
        a.error('请选择自提日期');
        return false;
      }
      t.data.mention_time = a.time_str(t.date + ' ' + t.time);
      t.data.mailing_type = 2;
    } else if (t.send_type == 'endpay') {
      if (!t.address.phone || !t.address.province) {
        a.error('请选择收货地址！');
        return false;
      }
      t.data.mailing_type = 4;
    } else {
      if (!t.address.phone || !t.address.province) {
        a.error('请选择收货地址！');
        return false;
      }
      t.data.mailing_type = 1;
    }

    if (t.disabled) {
      return false;
    }
    console.log("data",t.data);
    e.setData({
      disabled: true
    }),
    a.get("Miaosha/CreateOrder", t.data, function (t) {
      if(0 == t.code ){
        //推送
        a.get('Wxpush/CreateOrderPush4', {
          order_id: t.info,
          formid: formId,
          uid: getApp().getCache("userinfo").uid,
        }, function (tt) {

          // console.log(tt)
          // console.log(22222222);
        })
        e.setData({
          submit: false,
          order_id: t.info,
          disabled: false
        })
        // 订单金额为0时调用接口，支付成功
        if (e.data.data.pay_money == 0) {
          a.get("pay/updPayStatus", {
            order_id: t.info,
            pay_type: 3
          }, function(res) {
            if (res.code == 1) {
              wx.redirectTo({
                url: "/yb_mingpian/pages/miaosha/order/pay/index?id=" + t.info
              })
            } else {
              console.log("update_order",res);
              a.alert(res.msg)
            }
          })
        } else {
          wx.redirectTo({
            url: "/yb_mingpian/pages/miaosha/order/pay/index?id=" + t.info
          })
        }

      }else{
        e.setData({
          disabled: false
        }),
        a.alert(t.msg)
      }
    })
  },

  // 信息输入
  dataChange: function (t) {
    var e = this.data.data,
      a = this.data.list;
    switch (t.target.id) {
      case "buyer_message":  //留言
        e.buyer_message = t.detail.value;
        break;
      case "consigner":
        e.consigner = t.detail.value;
        break;
      case "phone":
        e.phone = t.detail.value;
        break;
    }
    this.setData({
      data: e,
      //list: a
    })
  },

  // 地址跳转
  url: function (t) {
    var e = a.pdata(t).url;
    wx.redirectTo({
      url: e
    })
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  
})